package com.zr.action;

import java.io.IOException;
import java.io.Writer;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zr.model.Func;
import com.zr.model.Role;
import com.zr.model.User;
import com.zr.service.AddCarService;
import com.zr.service.AddRoleService;

import net.sf.json.JSONArray;

@Controller
@Scope(value="prototype")
public class RoleAction implements ServletResponseAware{
private Object[] func;
private String rname;
private int UserName;
private int UserRole;



public int getUserName() {
	return UserName;
}

public void setUserName(int userName) {
	UserName = userName;
}
public int getUserRole() {
	return UserRole;
}

public void setUserRole(int userRole) {
	UserRole = userRole;
}

public Object[] getFunc() {
	return func;
}

public void setFunc(Object[] func) {
	this.func = func;
}

public String getRname() {
	return rname;
}

public void setRname(String rname) {
	this.rname = rname;
}
@Resource
private  AddRoleService  addRoleService;
private ServletResponse req;
public void addRole(){
	String str2=StringUtils.join(func, ",");
	Role role=addRoleService.addRole(rname, str2);
	if(role!=null){
		try {
			Writer ps=req.getWriter();
			ps.write("1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
public void findRole(){
	req.setCharacterEncoding("utf8");
JSONArray js= addRoleService.findRole();
try {
	Writer ps=req.getWriter();
	ps.write(js.toString());
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
}
public void findUser(){
	req.setCharacterEncoding("utf8");
JSONArray js= addRoleService.findUser();
try {
	Writer ps=req.getWriter();
	ps.write(js.toString());
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
}
public void updateRole(){	
	boolean i=addRoleService.updateRole(UserName, UserRole);
	if(i==true){
		Writer ps;
		try {
			ps = req.getWriter();
			ps.write("1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
@Override
public void setServletResponse(HttpServletResponse Response) {
	this.req=Response;
	
}
}
